﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class CarrierBLL : IBaseInfo
    {
        private static readonly ICarrierDA CarrierDA = DataAccess.CreateCarrierDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;

        public CarrierBLL()
        {
            this.pubParasInfo = null;
        }

        public CarrierBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public CarrierBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string CarrierImportByData(string[][] ImportByData, string IsUpdate)
        {
            string carrierIDTrue = string.Empty;
            string carrierIDFalse = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 1; ImportByData.Length > i; i++)
                {
                    CarrierInfo carrier = new CarrierInfo();
                    for (int n = 0; ImportByData[i].Length > n; n++)
                    {
                        try
                        {
                            switch (ImportByData[0][n])
                            {
                                case "承运商代码":
                                {
                                    if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                    {
                                        carrier.CarrierID = ImportByData[i][n];
                                    }
                                    continue;
                                }
                                case "承运商名称":
                                {
                                    carrier.Company = ImportByData[i][n];
                                    continue;
                                }
                                case "描述":
                                {
                                    carrier.Description = ImportByData[i][n];
                                    continue;
                                }
                                case "增值税号":
                                {
                                    carrier.Vat = ImportByData[i][n];
                                    continue;
                                }
                                case "城市":
                                {
                                    carrier.City = ImportByData[i][n];
                                    continue;
                                }
                                case "省份":
                                {
                                    carrier.State = ImportByData[i][n];
                                    continue;
                                }
                                case "邮编":
                                {
                                    carrier.Zip = ImportByData[i][n];
                                    continue;
                                }
                                case "国家":
                                {
                                    carrier.Country = ImportByData[i][n];
                                    continue;
                                }
                                case "ISO国家码":
                                {
                                    carrier.CountryCode = ImportByData[i][n];
                                    continue;
                                }
                                case "地址行":
                                {
                                    carrier.Address = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人1":
                                {
                                    carrier.Contact1 = ImportByData[i][n];
                                    continue;
                                }
                                case "电话1":
                                {
                                    carrier.Phone1 = ImportByData[i][n];
                                    continue;
                                }
                                case "传真1":
                                {
                                    carrier.Fax1 = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人1电子邮件":
                                {
                                    carrier.Email1 = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人2":
                                {
                                    carrier.Contact2 = ImportByData[i][n];
                                    continue;
                                }
                                case "电话2":
                                {
                                    carrier.Phone2 = ImportByData[i][n];
                                    continue;
                                }
                                case "传真2":
                                {
                                    carrier.Fax2 = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人2电子邮件":
                                {
                                    carrier.Email2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B城市":
                                {
                                    carrier.BCity = ImportByData[i][n];
                                    continue;
                                }
                                case "B省份":
                                {
                                    carrier.BState = ImportByData[i][n];
                                    continue;
                                }
                                case "B邮编":
                                {
                                    carrier.BZip = ImportByData[i][n];
                                    continue;
                                }
                                case "B国家":
                                {
                                    carrier.BCountry = ImportByData[i][n];
                                    continue;
                                }
                                case "BISO国家码":
                                {
                                    carrier.BCountryCode = ImportByData[i][n];
                                    continue;
                                }
                                case "B账单地址":
                                {
                                    carrier.BAddress = ImportByData[i][n];
                                    continue;
                                }
                                case "B账单联系人1":
                                {
                                    carrier.BContact1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电话1":
                                {
                                    carrier.BPhone1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B传真1":
                                {
                                    carrier.BFax1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电子邮件1":
                                {
                                    carrier.BEmail1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B账单联系人2":
                                {
                                    carrier.BContact2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电话2":
                                {
                                    carrier.BPhone2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B传真2":
                                {
                                    carrier.BFax2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电子邮件2":
                                {
                                    carrier.BEmail2 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段1":
                                {
                                    carrier.Udf1 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段2":
                                {
                                    carrier.Udf2 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段3":
                                {
                                    carrier.Udf3 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段4":
                                {
                                    carrier.Udf4 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段5":
                                {
                                    carrier.Udf5 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段6":
                                {
                                    carrier.Udf6 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段7":
                                {
                                    carrier.Udf7 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段8":
                                {
                                    carrier.Udf8 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段9":
                                {
                                    carrier.Udf9 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段10":
                                {
                                    carrier.Udf10 = ImportByData[i][n];
                                    continue;
                                }
                                case "备注":
                                {
                                    carrier.Remark = ImportByData[i][n];
                                    continue;
                                }
                                case "创建人":
                                {
                                    carrier.CreatedBy = ImportByData[i][n];
                                    continue;
                                }
                            }
                        }
                        catch
                        {
                            if (!string.IsNullOrEmpty(carrier.CarrierID))
                            {
                                carrierIDFalse = carrierIDFalse + carrier.CarrierID + PublicMethod.GetInstance().GetGlobalMsgResource("C003", this.pubParasInfo.UserLanguage) + "<br/>";
                            }
                        }
                    }
                    carrier.IsActive = "N";
                    carrier.UpdatedBy = carrier.CreatedBy;
                    if (!string.IsNullOrEmpty(carrier.CarrierID))
                    {
                        try
                        {
                            if (CarrierDA.CheckCarrierIDUnique(dataBase, tran, carrier.CarrierID, this.dataBaseID[0]))
                            {
                                if (!CarrierDA.CheckAuthorizationCarrierUserID(dataBase, tran, carrier.CarrierID, this.dataBaseID[0], this.pubParasInfo))
                                {
                                    carrierIDFalse = carrierIDFalse + carrier.CarrierID + PublicMethod.GetInstance().GetGlobalMsgResource("C008", this.pubParasInfo.UserLanguage) + "<br/>";
                                }
                                else if (IsUpdate == "Y")
                                {
                                    if (CarrierDA.UpdateCarrier(dataBase, tran, carrier, this.dataBaseID) > 0)
                                    {
                                        carrierIDFalse = carrierIDFalse + carrier.CarrierID + PublicMethod.GetInstance().GetGlobalMsgResource("C004", this.pubParasInfo.UserLanguage) + "<br/>";
                                    }
                                }
                                else
                                {
                                    carrierIDFalse = carrierIDFalse + carrier.CarrierID + PublicMethod.GetInstance().GetGlobalMsgResource("C005", this.pubParasInfo.UserLanguage) + "<br/>";
                                }
                            }
                            else if (CarrierDA.InsertCarrier(dataBase, tran, carrier, this.dataBaseID) > 0)
                            {
                                carrierIDTrue = carrierIDTrue + carrier.CarrierID + ";";
                            }
                        }
                        catch
                        {
                            if (!string.IsNullOrEmpty(carrier.CarrierID))
                            {
                                carrierIDFalse = carrierIDFalse + carrier.CarrierID + PublicMethod.GetInstance().GetGlobalMsgResource("C006", this.pubParasInfo.UserLanguage) + "<br/>";
                            }
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return carrierIDFalse;
        }

        public string Delete(List<string> CarrierIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<CarrierInfo> carrierInfos = new List<CarrierInfo>();
                CarrierInfo carrierInfo = null;
                foreach (string id in CarrierIDS)
                {
                    carrierInfo = CarrierDA.GetCarrierByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    carrierInfos.Add(carrierInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (CarrierDA.DeleteCarrier(dataBase, tran, CarrierIDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < CarrierIDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<CarrierInfo>(dataBase, tran, carrierInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "CARRIER", this.dataBaseID);
                        temp = temp + CarrierIDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return CarrierDA.GetAllCarrier(this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetInfoByID(string CarrierID)
        {
            return CarrierDA.GetCarrierByID(CarrierID, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            CarrierQueryEntity CarrierQuery = obj as CarrierQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(CarrierQuery.CarrierID))
            {
                sqlWhere.Add(" C.CARRIER_ID " + CarrierQuery.CarrierID);
            }
            if (!string.IsNullOrEmpty(CarrierQuery.Company))
            {
                sqlWhere.Add(" C.COMPANY " + CarrierQuery.Company);
            }
            if (!string.IsNullOrEmpty(CarrierQuery.City))
            {
                sqlWhere.Add(" C.CITY " + CarrierQuery.City);
            }
            if (!string.IsNullOrEmpty(CarrierQuery.Address))
            {
                sqlWhere.Add(" C.ADDRESS " + CarrierQuery.Address);
            }
            if (!string.IsNullOrEmpty(CarrierQuery.IsActive))
            {
                sqlWhere.Add(" C.IS_ACTIVE " + CarrierQuery.IsActive);
            }
            return CarrierDA.GetCarrierByQueryList(sqlWhere, CarrierQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            string CarrierID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                CarrierInfo Carrier = obj as CarrierInfo;
                if (CarrierDA.CheckCarrierIDUnique(dataBase, tran, Carrier.CarrierID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2140_001", this.pubParasInfo.UserLanguage, new List<string> { Carrier.CarrierID });
                }
                Carrier.UpdatedBy = Carrier.CreatedBy;
                Carrier.CreatedDate = DateTime.Now;
                Carrier.UpdatedDate = DateTime.Now;
                if (CarrierDA.InsertCarrier(dataBase, tran, Carrier, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<CarrierInfo>(dataBase, tran, null, Carrier, AuditAction.Insert, this.pubParasInfo.UserID, "CARRIER", this.dataBaseID);
                    CarrierID = Carrier.CarrierID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return CarrierID;
        }

        public string Update(object obj)
        {
            string CarrierID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                CarrierInfo Carrier = obj as CarrierInfo;
                Carrier.UpdatedDate = DateTime.Now;
                CarrierInfo oldCarrierInfo = CarrierDA.GetCarrierByID(dataBase, tran, Carrier.CarrierID, this.dataBaseID[0], this.pubParasInfo);
                if (CarrierDA.UpdateCarrier(dataBase, tran, Carrier, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<CarrierInfo>(dataBase, tran, oldCarrierInfo, Carrier, AuditAction.Update, this.pubParasInfo.UserID, "CARRIER", this.dataBaseID);
                    CarrierID = Carrier.CarrierID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return CarrierID;
        }
    }
}

